컴퓨터 과학에서, 포인터 는 기본적인 형태의 간접 참조. 값을 직접 보관하는 대신, 포인터 변수는 메모리 주소—그 값이 저장된 램 내 특정 위치—를 저장합니다. 이는 비용이 큰 데이터 복제 없이 하나의 진실 원본에 대한 변경 사항을 조율할 수 있게 해줍니다.
1. 주소의 논리
값이 저장되는 위치는 그 값의 메모리 주소라고 알려져 있습니다. 이를 이해하는 것은 컴퓨터의 내부 언어를 말하는 첫걸음입니다. Go에서는 앰퍼샌드 (&)로 주소를 찾고, 별표 (*)로 해당 주소를 따라갑니다.
2. 왜 간접 참조가 중요한가
간접 참조는 복잡하고 공유되는 데이터 구조를 만들 때 강력한 도구입니다. 새로운 주소로 방문자를 안내하는 상점 표지판을 생각해보세요. 표지판은 상점을 포함하지 않습니다; 그저 당신이 어디 -looking을 알려줍니다. Go는 이 기술을 익히는 안전한 환경을 제공합니다: 포인터를 본 적 있다면 깊게 숨을 들이쉬세요. 그렇게 나쁘진 않을 거예요. 처음 접하는 경우라면 마음을 편하게 두세요. Go는 포인터를 배우기에 안전한 장소입니다.
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Like the shop sign directing visitors to a new address, pointers direct a computer where to look for a value. What's another situation where you're directed to look somewhere else?
A URL redirecting to a new website.
Reading the final page of a book directly.
Calculating 2 + 2 in your head.
A static variable that never changes.
✅ Correct!
Correct! URLs, library index cards, and table of contents are all real-world forms of indirection.❌ Incorrect
Think about things that act as references to other locations rather than the content itself.QUESTION 2
How do you know that
time.Time never uses a pointer receiver?It is an immutable type that returns a new value for every method call.
It is a primitive type like int.
The Go compiler prohibits pointers on time structs.
It uses global variables instead.
✅ Correct!
Exactly. Methods like .Add return a new time.Time instead of modifying the existing one, which is characteristic of value receivers.❌ Incorrect
Check the documentation: methods return a new instance of Time rather than mutating the current one.QUESTION 3
What is the result of using the address operator (
&) on a variable?It returns the value stored in the variable.
It returns the memory address (location) of the variable.
It doubles the memory usage of the variable.
It deletes the variable from memory.
✅ Correct!
The ampersand is the 'address-of' operator.❌ Incorrect
The address operator finds the 'where', not the 'what'.QUESTION 4
Quick check 26.1: If a pointer is a signpost, what happens if the building it points to is painted a different color? Does the signpost need to change?
Yes, because the signpost must describe the color.
No, because the address (location) remains the same.
Yes, the pointer automatically breaks.
No, pointers only point to grayscale buildings.
✅ Correct!
Correct. Changing the data at the address doesn't change the address itself.❌ Incorrect
The pointer only cares about the location, not the internal state of the data stored there.QUESTION 5
Which statement best describes Go's approach to pointers?
It allows dangerous pointer arithmetic like C.
It is a safe environment that handles many complexities for you.
Pointers are not allowed in Go.
Pointers only work with integers.
✅ Correct!
Go is designed to be a safe place to learn and use pointers without many of the pitfalls of lower-level languages.❌ Incorrect
Review the 'Tips' section: Go is a safe place to learn pointers.Module 7 Challenge: Indirection and Sudoku Logic
Applying Pointers to Shared State Management
You are tasked with building a Sudoku validation engine. In Sudoku, the grid must be shared across various validation rules (row, column, subgrid). Passing the entire 81-element array by value would be inefficient. You must use pointer receivers to mutate the grid and handle errors gracefully.
Q
1. Write a line of code to sort a slice named 'food' from the shortest to longest string using 'sort.Slice' and a closure. [Word Count Requirement: 15 words]
Solution:
sort.Slice(food, func(i, j int) bool { return len(food[i]) < len(food[j]) })
sort.Slice(food, func(i, j int) bool { return len(food[i]) < len(food[j]) })
Q
2. If an error occurred while writing 'Clear is better than clever.' to a file in Listing 28.6, what sequence of events would follow?
Solution:
The error would be stored in the struct's error field. Subsequent write calls would check this field, find a non-nil error, and return immediately without attempting further writes, ensuring the first error is preserved.
The error would be stored in the struct's error field. Subsequent write calls would check this field, find a non-nil error, and return immediately without attempting further writes, ensuring the first error is preserved.
Q
3. Implement a method signature to clear a digit from a square in a Sudoku struct. This method need not adhere to constraints, as several squares may be empty (zero).
Solution:
func (s *Sudoku) Clear(row, col int) { s.grid[row][col] = 0 }
func (s *Sudoku) Clear(row, col int) { s.grid[row][col] = 0 }